<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>21.5. Zend_Json_Server - JSON-RPC server</title>
<link rel="stylesheet" href="dbstyle.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
<link rel="start" href="index.html" title="Programmer's Reference Guide">
<link rel="up" href="zend.json.html" title="Chapter 21. Zend_Json">
<link rel="prev" href="zend.json.xml2json.html" title="21.4. XML to JSON conversion">
<link rel="next" href="zend.layout.html" title="Chapter 22. Zend_Layout">
<link rel="chapter" href="introduction.html" title="Chapter 1. Introduction to Zend Framework">
<link rel="chapter" href="zend.acl.html" title="Chapter 2. Zend_Acl">
<link rel="chapter" href="zend.auth.html" title="Chapter 3. Zend_Auth">
<link rel="chapter" href="zend.cache.html" title="Chapter 4. Zend_Cache">
<link rel="chapter" href="zend.config.html" title="Chapter 5. Zend_Config">
<link rel="chapter" href="zend.console.getopt.html" title="Chapter 6. Zend_Console_Getopt">
<link rel="chapter" href="zend.controller.html" title="Chapter 7. Zend_Controller">
<link rel="chapter" href="zend.currency.html" title="Chapter 8. Zend_Currency">
<link rel="chapter" href="zend.date.html" title="Chapter 9. Zend_Date">
<link rel="chapter" href="zend.db.html" title="Chapter 10. Zend_Db">
<link rel="chapter" href="zend.debug.html" title="Chapter 11. Zend_Debug">
<link rel="chapter" href="zend.dojo.html" title="Chapter 12. Zend_Dojo">
<link rel="chapter" href="zend.dom.html" title="Chapter 13. Zend_Dom">
<link rel="chapter" href="zend.exception.html" title="Chapter 14. Zend_Exception">
<link rel="chapter" href="zend.feed.html" title="Chapter 15. Zend_Feed">
<link rel="chapter" href="zend.filter.html" title="Chapter 16. Zend_Filter">
<link rel="chapter" href="zend.form.html" title="Chapter 17. Zend_Form">
<link rel="chapter" href="zend.gdata.html" title="Chapter 18. Zend_Gdata">
<link rel="chapter" href="zend.http.html" title="Chapter 19. Zend_Http">
<link rel="chapter" href="zend.infocard.html" title="Chapter 20. Zend_InfoCard">
<link rel="chapter" href="zend.json.html" title="Chapter 21. Zend_Json">
<link rel="chapter" href="zend.layout.html" title="Chapter 22. Zend_Layout">
<link rel="chapter" href="zend.ldap.html" title="Chapter 23. Zend_Ldap">
<link rel="chapter" href="zend.loader.html" title="Chapter 24. Zend_Loader">
<link rel="chapter" href="zend.locale.html" title="Chapter 25. Zend_Locale">
<link rel="chapter" href="zend.log.html" title="Chapter 26. Zend_Log">
<link rel="chapter" href="zend.mail.html" title="Chapter 27. Zend_Mail">
<link rel="chapter" href="zend.measure.html" title="Chapter 28. Zend_Measure">
<link rel="chapter" href="zend.memory.html" title="Chapter 29. Zend_Memory">
<link rel="chapter" href="zend.mime.html" title="Chapter 30. Zend_Mime">
<link rel="chapter" href="zend.openid.html" title="Chapter 31. Zend_OpenId">
<link rel="chapter" href="zend.paginator.html" title="Chapter 32. Zend_Paginator">
<link rel="chapter" href="zend.pdf.html" title="Chapter 33. Zend_Pdf">
<link rel="chapter" href="zend.registry.html" title="Chapter 34. Zend_Registry">
<link rel="chapter" href="zend.rest.html" title="Chapter 35. Zend_Rest">
<link rel="chapter" href="zend.search.lucene.html" title="Chapter 36. Zend_Search_Lucene">
<link rel="chapter" href="zend.server.html" title="Chapter 37. Zend_Server">
<link rel="chapter" href="zend.service.html" title="Chapter 38. Zend_Service">
<link rel="chapter" href="zend.session.html" title="Chapter 39. Zend_Session">
<link rel="chapter" href="zend.soap.html" title="Chapter 40. Zend_Soap">
<link rel="chapter" href="zend.test.html" title="Chapter 41. Zend_Test">
<link rel="chapter" href="zend.text.html" title="Chapter 42. Zend_Text">
<link rel="chapter" href="zend.timesync.html" title="Chapter 43. Zend_TimeSync">
<link rel="chapter" href="zend.translate.html" title="Chapter 44. Zend_Translate">
<link rel="chapter" href="zend.uri.html" title="Chapter 45. Zend_Uri">
<link rel="chapter" href="zend.validate.html" title="Chapter 46. Zend_Validate">
<link rel="chapter" href="zend.version.html" title="Chapter 47. Zend_Version">
<link rel="chapter" href="zend.view.html" title="Chapter 48. Zend_View">
<link rel="chapter" href="zend.xmlrpc.html" title="Chapter 49. Zend_XmlRpc">
<link rel="appendix" href="requirements.html" title="Appendix A. Zend Framework Requirements">
<link rel="appendix" href="coding-standard.html" title="Appendix B. Zend Framework Coding Standard for PHP">
<link rel="appendix" href="copyrights.html" title="Appendix C. Copyright Information">
<link rel="index" href="the.index.html" title="Index">
<link rel="subsection" href="zend.json.server.html#zend.json.server.details" title="21.5.1. Advanced Details">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader"><table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">21.5. Zend_Json_Server - JSON-RPC server</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="zend.json.xml2json.html">Prev</a> </td>
<th width="60%" align="center">Chapter 21. Zend_Json</th>
<td width="20%" align="right"> <a accesskey="n" href="zend.layout.html">Next</a>
</td>
</tr>
</table></div>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="zend.json.server"></a>21.5. Zend_Json_Server - JSON-RPC server</h2></div></div></div>
<p>
        <code class="code">Zend_Json_Server</code> is a <a href="http://groups.google.com/group/json-rpc/" target="_top">JSON-RPC</a>
        server implementation. It supports both the 
        <a href="http://json-rpc.org/wiki/specification" target="_top">JSON-RPC
            version 1 specification</a> as well as the 
        <a href="http://groups.google.com/group/json-rpc/web/json-rpc-1-2-proposal" target="_top">version 2 specification</a>; 
        additionally, it provides a PHP implemention of the 
        <a href="http://groups.google.com/group/json-schema/web/service-mapping-description-proposal" target="_top">Service
            Mapping Description (SMD) specification</a> 
        for providing service metadata to service consumers.
    </p>
<p>
        JSON-RPC is a lightweight Remote Procedure Call protocol that utilizes
        JSON for its messaging envelopes. This JSON-RPC implementation follows
        PHP's <a href="http://us.php.net/manual/en/function.soap-soapserver-construct.php" target="_top">SoapServer</a>
        API. This means that in a typical situation, you will simply:
    </p>
<div class="itemizedlist"><ul type="disc">
<li><p>Instantiate the server object</p></li>
<li><p>Attach one or more functions and/or classes/objects to
                the server object</p></li>
<li><p>handle() the request</p></li>
</ul></div>
<p>
        <code class="code">Zend_Json_Server</code> utilizes <a href="zend.server.reflection.html" title="37.2. Zend_Server_Reflection">Section 37.2, “Zend_Server_Reflection”</a> to
        perform reflection on any attached classes or functions, and uses that
        information to build both the SMD and enforce method call signatures. As
        such, it is imperative that any attached functions and/or class methods
        have full PHP docblocks documenting, minimally:
    </p>
<div class="itemizedlist"><ul type="disc">
<li><p>All parameters and their expected variable types</p></li>
<li><p>The return value variable type</p></li>
</ul></div>
<p>
        <code class="code">Zend_Json_Server</code> listens for POST requests only at this
        time; fortunately, most JSON-RPC client implementations in the wild at
        the time of this writing will only POST requests as it is. This makes it
        simple to utilize the same server end point to both handle requests as
        well as to deliver the service SMD, as is shown in the next example.
    </p>
<div class="example">
<a name="zend.json.server.usage"></a><p class="title"><b>Example 21.1. Zend_Json_Server Usage</b></p>
<div class="example-contents">
<p>
            First, let's define a class we wish to expose via the JSON-RPC
            server. We'll call the class 'Calculator', and define methods for
            'add', 'subtract', 'multiply', and 'divide':
        </p>
<pre class="programlisting">
/**
 * Calculator - sample class to expose via JSON-RPC
 */
class Calculator
{
    /**
     * Return sum of two variables
     * 
     * @param  int $x 
     * @param  int $y 
     * @return int
     */
    public function add($x, $y)
    {
        return $x + $y;
    }

    /**
     * Return difference of two variables
     * 
     * @param  int $x 
     * @param  int $y 
     * @return int
     */
    public function subtract($x, $y)
    {
        return $x - $y;
    }

    /**
     * Return product of two variables 
     * 
     * @param  int $x 
     * @param  int $y 
     * @return int
     */
    public function multiply($x, $y)
    {
        return $x * $y;
    }

    /**
     * Return the product of division of two variables
     * 
     * @param  int $x 
     * @param  int $y 
     * @return float
     */
    public function divide($x, $y)
    {
        return $x / $y;
    }
}
?&gt;</pre>
<p>
            Note that each method has a docblock with entries indicating each
            parameter and its type, as well as an entry for the return value.
            This is <span class="emphasis"><em>absolutely critical</em></span> when utilizing
            <code class="code">Zend_Json_Server</code> -- or any other server component in
            Zend Framework, for that matter.
        </p>
<p>
            Now we'll create a script to handle the requests:
        </p>
<pre class="programlisting">
$server = new Zend_Json_Server();

// Indicate what functionality is available:
$server-&gt;setClass('Calculator');

// Handle the request:
$server-&gt;handle();
?&gt;</pre>
<p>
            However, this will not address the issue of returning an SMD so that
            the JSON-RPC client can autodiscover methods. That can be
            accomplished by determining the HTTP request method, and then
            specifying some server metadata:
        </p>
<pre class="programlisting">
$server = new Zend_Json_Server();
$server-&gt;setClass('Calculator');

if ('GET' == $_SERVER['REQUEST_METHOD']) {
    // Indicate the URL endpoint, and the JSON-RPC version used:
    $server-&gt;setTarget('/json-rpc.php')
           -&gt;setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);

    // Grab the SMD
    $smd = $server-&gt;getServiceMap();

    // Return the SMD to the client
    header('Content-Type: application/json');
    echo $smd;
    return;
}

$server-&gt;handle();
?&gt;</pre>
<p>
            If utilizing the JSON-RPC server with Dojo toolkit, you will also
            need to set a special compatibility flag to ensure that the two
            interoperate properly:
        </p>
<pre class="programlisting">
$server = new Zend_Json_Server();
$server-&gt;setClass('Calculator');

if ('GET' == $_SERVER['REQUEST_METHOD']) {
    $server-&gt;setTarget('/json-rpc.php')
           -&gt;setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
    $smd = $server-&gt;getServiceMap();

    // Set Dojo compatibility:
    $smd-&gt;setDojoCompatible(true);

    header('Content-Type: application/json');
    echo $smd;
    return;
}

$server-&gt;handle();
?&gt;</pre>
</div>
</div>
<br class="example-break"><div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.json.server.details"></a>21.5.1. Advanced Details</h3></div></div></div>
<p>
            While most functionality for <code class="code">Zend_Json_Server</code> is
            spelled out in <a href="zend.json.server.html#zend.json.server.usage" title="Example 21.1. Zend_Json_Server Usage">Example 21.1, “Zend_Json_Server Usage”</a>, more
            advanced functionality is available.
        </p>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.json.server.details.zendjsonserver"></a>21.5.1.1. Zend_Json_Server</h4></div></div></div>
<p>
                <code class="code">Zend_Json_Server</code> is the core class in the JSON-RPC
                offering; it handles all requests and returns the response
                payload. It has the following methods:
            </p>
<div class="itemizedlist"><ul type="disc">
<li><p><code class="code">addFunction($function)</code>: Specify a
                        userland function to attach to the server.</p></li>
<li><p><code class="code">setClass($class)</code>: Specify a class
                        or object to attach to the server; all public methods of
                        that item will be exposed as JSON-RPC methods.</p></li>
<li><p><code class="code">fault($fault = null, $code = 404, $data =
                        null)</code>: Create and return a
                        <code class="code">Zend_Json_Server_Error</code> object.</p></li>
<li><p><code class="code">handle($request = false)</code>: Handle a
                        JSON-RPC request; optionally, pass a
                        <code class="code">Zend_Json_Server_Request</code> object to utilize
                        (creates one by default).</p></li>
<li><p><code class="code">getFunctions()</code>: Return a list of
                        all attached methods.</p></li>
<li><p><code class="code">setRequest(Zend_Json_Server_Request
                        $request)</code>: Specify a request object for the
                        server to utilize.</p></li>
<li><p><code class="code">getRequest()</code>: Retrieve the request
                        object used by the server.</p></li>
<li><p><code class="code">setResponse(Zend_Json_Server_Response
                        $response)</code>: Set the response object for the
                        server to utilize.</p></li>
<li><p><code class="code">getResponse()</code>: Retrieve the
                        response object used by the server.</p></li>
<li><p><code class="code">setAutoEmitResponse($flag)</code>:
                        Indicate whether the server should automatically emit
                        the response and all headers; by default, this is
                        true.</p></li>
<li><p><code class="code">autoEmitResponse()</code>: Determine if
                        auto-emission of the response is enabled.</p></li>
<li><p><code class="code">getServiceMap()</code>: Retrieve the
                        service map description in the form of a
                        <code class="code">Zend_Json_Server_Smd</code> object</p></li>
</ul></div>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.json.server.details.zendjsonserverrequest"></a>21.5.1.2. Zend_Json_Server_Request</h4></div></div></div>
<p>
                The JSON-RPC request environment is encapsulated in the
                <code class="code">Zend_Json_Server_Request</code> object. This object allows
                you to set necessary portions of the JSON-RPC request, including
                the request ID, parameters, and JSON-RPC specification version.
                It has the ability to load itself via JSON or a set of options,
                and can render itself as JSON via the <code class="code">toJson()</code>
                method.
            </p>
<p>
                The request object has the following methods available:
            </p>
<div class="itemizedlist"><ul type="disc">
<li><p><code class="code">setOptions(array $options)</code>: Specify
                        object configuration. <code class="code">$options</code> may contain
                        keys matching any 'set' method:
                        <code class="code">setParams()</code>, <code class="code">setMethod()</code>,
                        <code class="code">setId()</code>, and
                        <code class="code">setVersion()</code>.</p></li>
<li><p><code class="code">addParam($value, $key = null)</code>: Add
                        a parameter to use with the method call. Parameters can be
                        just the values, or can optionally include the parameter
                        name.</p></li>
<li><p><code class="code">addParams(array $params)</code>: Add
                        multiple parameters at once; proxies to
                        <code class="code">addParam()</code></p></li>
<li><p><code class="code">setParams(array $params)</code>: Set all
                        parameters at once; overwrites any existing
                        parameters.</p></li>
<li><p><code class="code">getParam($index)</code>: Retrieve a
                        parameter by position or name.</p></li>
<li><p><code class="code">getParams()</code>: Retrieve all
                        parameters at once.</p></li>
<li><p><code class="code">setMethod($name)</code>: Set the method to
                        call.</p></li>
<li><p><code class="code">getMethod()</code>: Retrieve the method
                        that will be called.</p></li>
<li><p><code class="code">isMethodError()</code>: Determine whether
                        or not the request is malformed and would result in an
                        error.</p></li>
<li><p><code class="code">setId($name)</code>: Set the request
                        identifier (used by the client to match requests to
                        responses).</p></li>
<li><p><code class="code">getId()</code>: Retrieve the request
                        identifier.</p></li>
<li><p><code class="code">setVersion($version)</code>: Set the
                        JSON-RPC specification version the request conforms to.
                        May be either '1.0' or '2.0'.</p></li>
<li><p><code class="code">getVersion()</code>: Retrieve the JSON-RPC
                        specification version used by the
                        request.</p></li>
<li><p><code class="code">loadJson($json)</code>: Load the request
                        object from a JSON string.</p></li>
<li><p><code class="code">toJson()</code>: Render the request as
                        a JSON string.</p></li>
</ul></div>
<p>
                An HTTP specific version is available via
                <code class="code">Zend_Json_Server_Request_Http</code>. This class will
                retrieve the request via <code class="code">php://input</code>, and allows
                access to the raw JSON via the <code class="code">getRawJson()</code> method.
            </p>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.json.server.details.zendjsonserverresponse"></a>21.5.1.3. Zend_Json_Server_Response</h4></div></div></div>
<p>
                The JSON-RPC response payload is encapsulated in the
                <code class="code">Zend_Json_Server_Response</code> object. This object allows
                you to set the return value of the request, whether or not the
                response is an error, the request identifier, the JSON-RPC
                specification version the response conforms to, and optionally
                the service map.
            </p>
<p>
                The response object has the following methods available:
            </p>
<div class="itemizedlist"><ul type="disc">
<li><p><code class="code">setResult($value)</code>: Set the response
                    result.</p></li>
<li><p><code class="code">getResult()</code>: Retrieve the response
                    result.</p></li>
<li><p><code class="code">setError(Zend_Json_Server_Error
                    $error)</code>: Set an error object. If set, this will be
                    used as the response when serializing to JSON.</p></li>
<li><p><code class="code">getError()</code>: Retrieve the error
                    object, if any.</p></li>
<li><p><code class="code">isError()</code>: Whether or not the
                    response is an error response.</p></li>
<li><p><code class="code">setId($name)</code>: Set the request
                    identifier (so the client may match the response with
                    the original request).</p></li>
<li><p><code class="code">getId()</code>: Retrieve the request
                    identifier.</p></li>
<li><p><code class="code">setVersion($version)</code>: Set the
                    JSON-RPC version the response conforms to.</p></li>
<li><p><code class="code">getVersion()</code>: Retrieve the JSON-RPC
                    version the response conforms to.</p></li>
<li><p><code class="code">toJson()</code>: Serialize the response to
                    JSON. If the response is an error response, serializes the
                    error object.</p></li>
<li><p><code class="code">setServiceMap($serviceMap)</code>: Set the
                    service map object for the response.</p></li>
<li><p><code class="code">getServiceMap()</code>: Retrieve the
                    service map object, if any.</p></li>
</ul></div>
<p>
                An HTTP specific version is available via
                <code class="code">Zend_Json_Server_Response_Http</code>. This class will
                send the appropriate HTTP headers as well as serialize the
                response as JSON.
            </p>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.json.server.details.zendjsonservererror"></a>21.5.1.4. Zend_Json_Server_Error</h4></div></div></div>
<p>
                JSON-RPC has a special format for reporting error conditions.
                All errors need to provide, minimally, an error message and error
                code; optionally, they can provide additional data, such as a
                backtrace.
            </p>
<p>
                Error codes are derived from those recommended by <a href="http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php" target="_top">the
                    XML-RPC EPI project</a>. <code class="code">Zend_Json_Server</code>
                appropriately assigns the code based on the error condition. For
                application exceptions, the code '-32000' is used.
            </p>
<p>
                <code class="code">Zend_Json_Server_Error</code> exposes the following
                methods:
            </p>
<div class="itemizedlist"><ul type="disc">
<li><p><code class="code">setCode($code)</code>: Set the error code;
                    if the code is not in the accepted XML-RPC error code range,
                    -32000 will be assigned.</p></li>
<li><p><code class="code">getCode()</code>: Retrieve the current
                    error code.</p></li>
<li><p><code class="code">setMessage($message)</code>: Set the error
                    message.</p></li>
<li><p><code class="code">getMessage()</code>: Retrieve the current
                    error message.</p></li>
<li><p><code class="code">setData($data)</code>: Set auxiliary data
                    further qualifying the error, such as a
                    backtrace.</p></li>
<li><p><code class="code">getData()</code>: Retrieve any current
                    auxiliary error data.</p></li>
<li><p><code class="code">toArray()</code>: Cast the error to an
                    array. The array will contain the keys 'code', 'message',
                    and 'data'.</p></li>
<li><p><code class="code">toJson()</code>: Cast the error to a
                    JSON-RPC error representation.</p></li>
</ul></div>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.json.server.details.zendjsonserversmd"></a>21.5.1.5. Zend_Json_Server_Smd</h4></div></div></div>
<p>
                SMD stands for Service Mapping Description, a JSON schema that
                defines how a client can interact with a particular web service.
                At the time of this writing, the <a href="http://groups.google.com/group/json-schema/web/service-mapping-description-proposal" target="_top">specification</a>
                has not yet been formally ratified, but it is in use already
                within Dojo toolkit as well as other JSON-RPC consumer clients.
            </p>
<p>
                At its most basic, a Service Mapping Description indicates the
                method of transport (POST, GET, TCP/IP, etc), the request
                envelope type (usually based on the protocol of the server), the
                target URL of the service provider, and a map of services
                available. In the case of JSON-RPC, the service map is a list of
                available methods, which each method documenting the available
                parameters and their types, as well as the expected return value
                type.
            </p>
<p>
                <code class="code">Zend_Json_Server_Smd</code> provides an object oriented
                way to build service maps. At its most basic, you pass it
                metadata describing the service using mutators, and specify
                services (methods and functions).
            </p>
<p>
                The service descriptions themselves are typically instances of
                <code class="code">Zend_Json_Server_Smd_Service</code>; you can also pass all
                information as an array to the various service mutators in
                <code class="code">Zend_Json_Server_Smd</code>, and it will instantiate a
                service object for you. The service objects contain information
                such as the name of the service (typically the function or
                method name), the parameters (names, types, and position), and
                the return value type. Optionally, each service can have its own
                target and envelope, though this functionality is rarely used. 
            </p>
<p>
                <code class="code">Zend_Json_Server</code> actually does all of this behind
                the scenes for you, by using reflection on the attached classes
                and functions; you should create your own service maps only if
                you need to provide custom functionality that class and function
                introspection cannot offer.
            </p>
<p>
                Methods available in <code class="code">Zend_Json_Server_Smd</code> include:
            </p>
<div class="itemizedlist"><ul type="disc">
<li><p><code class="code">setOptions(array $options)</code>: Setup
                        an SMD object from an array of options. All mutators
                        (methods beginning with 'set') can be used as
                        keys.</p></li>
<li><p><code class="code">setTransport($transport)</code>: Set the
                        transport used to access the service; only POST is
                        currently supported.</p></li>
<li><p><code class="code">getTransport()</code>: Get the current
                        service transport.</p></li>
<li><p><code class="code">setEnvelope($envelopeType)</code>: Set the
                        request envelope that should be used to access the
                        service. Currently, supports the constants
                        <code class="code">Zend_Json_Server_Smd::ENV_JSONRPC_1</code> and
                        <code class="code">Zend_Json_Server_Smd::ENV_JSONRPC_1</code>.</p></li>
<li><p><code class="code">getEnvelope()</code>: Get the current
                        request envelope.</p></li>
<li><p><code class="code">setContentType($type)</code>: Set the
                        content type requests should use (by default, this is
                        'application/json').</p></li>
<li><p><code class="code">getContentType()</code>: Get the current
                        content type for requests to the service.</p></li>
<li><p><code class="code">setTarget($target)</code>: Set the URL
                        endpoint for the service.</p></li>
<li><p><code class="code">getTarget()</code>: Get the URL endpoint
                        for the service.</p></li>
<li><p><code class="code">setId($id)</code>: Typically, this is the
                        URL endpoint of the service (same as the
                        target).</p></li>
<li><p><code class="code">getId()</code>: Retrieve the service ID
                        (typically the URL endpoint of the
                        service).</p></li>
<li><p><code class="code">setDescription($description)</code>: Set a
                        service description (typically narrative information
                        describing the purpose of the service).</p></li>
<li><p><code class="code">getDescription()</code>: Get the service
                        description.</p></li>
<li><p><code class="code">setDojoCompatible($flag)</code>: Set a
                        flag indicating whether or not the SMD is compatible
                        with Dojo toolkit. When true, the generated JSON SMD
                        will be formatted to comply with the format that Dojo's
                        JSON-RPC client expects.</p></li>
<li><p><code class="code">isDojoCompatible()</code>: Returns the
                        value of the Dojo compatability flag (false, by
                        default).</p></li>
<li><p><code class="code">addService($service)</code>: Add a service
                        to the map. May be an array of information to pass to
                        the constructor of
                        <code class="code">Zend_Json_Server_Smd_Service</code>, or an
                        instance of that class.</p></li>
<li><p><code class="code">addServices(array $services)</code>: Add
                        multiple services at once.</p></li>
<li><p><code class="code">setServices(array $services)</code>: Add
                        multiple services at once, overwriting any previously
                        set services.</p></li>
<li><p><code class="code">getService($name)</code>: Get a service by
                        its name.</p></li>
<li><p><code class="code">getServices()</code>: Get all attached
                        services.</p></li>
<li><p><code class="code">removeService($name)</code>: Remove a
                        service from the map.</p></li>
<li><p><code class="code">toArray()</code>: Cast the service map to
                        an array.</p></li>
<li><p><code class="code">toDojoArray()</code>: Cast the service map
                        to an array compatible with Dojo Toolkit.</p></li>
<li><p><code class="code">toJson()</code>: Cast the service map to a
                        JSON representation.</p></li>
</ul></div>
<p>
                <code class="code">Zend_Json_Server_Smd_Service</code> has the following
                methods:
            </p>
<div class="itemizedlist"><ul type="disc">
<li><p><code class="code">setOptions(array $options)</code>: Set
                        object state from an array. Any mutator (methods
                        beginning with 'set') may be used as a key and set via
                        this method.</p></li>
<li><p><code class="code">setName($name)</code>: Set the service
                        name (typically, the function or method name).</p></li>
<li><p><code class="code">getName()</code>: Retrieve the service
                        name.</p></li>
<li><p><code class="code">setTransport($transport)</code>: Set the
                        service transport (currently, only transports supported
                        by <code class="code">Zend_Json_Server_Smd</code> are allowed).</p></li>
<li><p><code class="code">getTransport()</code>: Retrieve the
                        current transport.</p></li>
<li><p><code class="code">setTarget($target)</code>: Set the URL
                        endpoint of the service (typically, this will be the
                        same as the overal SMD to which the service is
                        attached).</p></li>
<li><p><code class="code">getTarget()</code>: Get the URL endpoint
                        of the service.</p></li>
<li><p><code class="code">setEnvelope($envelopeType)</code>: Set the
                        service envelope (currently, only envelopes supported
                        by <code class="code">Zend_Json_Server_Smd</code> are allowed).</p></li>
<li><p><code class="code">getEnvelope()</code>: Retrieve the service
                        envelope type.</p></li>
<li>
<p><code class="code">addParam($type, array $options = array(),
                            $order = null)</code>: Add a parameter to the
                        service. By default, only the parameter type is
                        necessary. However, you may also specify the order, as
                        well as options such as:</p>
<div class="itemizedlist"><ul type="circle">
<li><p><span class="emphasis"><em>name</em></span>: the parameter
                            name</p></li>
<li><p><span class="emphasis"><em>optional</em></span>: whether
                            or not the parameter is optional</p></li>
<li><p><span class="emphasis"><em>default</em></span>: a default
                            value for the parameter</p></li>
<li><p><span class="emphasis"><em>description</em></span>: text
                            describing the parameter</p></li>
</ul></div>
</li>
<li><p><code class="code">addParams(array $params)</code>: Add
                    several parameters at once; each param should be an assoc
                    array containing minimally the key 'type' describing the
                    parameter type, and optionally the key 'order'; any other
                    keys will be passed as <code class="code">$options</code> to
                    <code class="code">addOption()</code>.</p></li>
<li><p><code class="code">setParams(array $params)</code>: Set many
                    parameters at once, overwriting any existing
                    parameters.</p></li>
<li><p><code class="code">getParams()</code>: Retrieve all currently
                    set parameters.</p></li>
<li><p><code class="code">setReturn($type)</code>: Set the return
                    value type of the service.</p></li>
<li><p><code class="code">getReturn()</code>: Get the return value
                    type of the service.</p></li>
<li><p><code class="code">toArray()</code>: Cast the service to an
                    array.</p></li>
<li><p><code class="code">toJson()</code>: Cast the service to a
                    JSON representation.</p></li>
</ul></div>
</div>
</div>
</div>
<div class="navfooter"><table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="zend.json.xml2json.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="zend.json.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="zend.layout.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">21.4. XML to JSON conversion </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Chapter 22. Zend_Layout</td>
</tr>
</table></div>
<div class="revinfo"></div>
</body>
</html>
